package edu.njfu.networkdemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import edu.njfu.networkdemo.model.App
import edu.njfu.networkdemo.model.Student
import edu.njfu.networkdemo.service.AppService
import edu.njfu.networkdemo.service.StudentService
import edu.njfu.networkdemo.utils.ServerCreator
import kotlinx.android.synthetic.main.activity_fourth.*
import kotlinx.coroutines.runBlocking
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.await
import retrofit2.converter.gson.GsonConverterFactory
import java.lang.Exception
import java.lang.RuntimeException
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine


class FourthActivity : AppCompatActivity() {

    suspend fun getStudents(){
        Log.e("Mainactivity","test")
        try{
            var await = ServerCreator.create<StudentService>().getStudentData().await()
            val studentList= await
            if(studentList!=null) {
                for (s in studentList) {
                    Log.e("Retrotfit2", s.toString())
                }
            }else{
                Log.e("Retrofit2","error")
            }
        }catch (e:Exception){
            e.printStackTrace()
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_fourth)
        getAppDataBtn.setOnClickListener {
             runBlocking {
                 getStudents()
             }
//            val studentService=ServerCreator.create<StudentService>();
//
//             studentService.getStudentData().enqueue(object: Callback<List<Student>> {
//                 override fun onFailure(call: Call<List<Student>>, t: Throwable) {
//                     t.printStackTrace()
//                 }
//
//                 override fun onResponse(
//                     call: Call<List<Student>>,
//                     response: Response<List<Student>>
//                 ) {
//                     var students = response.body()
//                     if(students!=null){
//                         for(s in students){
//                             Log.e("FourthActivity",s.toString() )
//                         }
//                     }
//                 }
//
//             })
//            val retrofit= Retrofit.Builder()
//                .baseUrl("http://106.15.192.167:8080/")
//                .addConverterFactory(GsonConverterFactory.create())
//                .build()
//            val studentService=retrofit.create(StudentService::class.java)
//            studentService.getStudentData().enqueue(object:Callback<List<Student>>{
//                override fun onFailure(call: Call<List<Student>>, t: Throwable) {
//                    t.printStackTrace()
//                }
//
//                override fun onResponse(call: Call<List<Student>>, response: Response<List<Student>>) {
//                     val list=response.body()
//                    if(list!=null){
//                        for(stu in list){
//                            Log.e("MainActivity","id is ${stu.toString()}")
//                        }
//                    }
//                    Log.e("FourthAcitvity","respone!")
//                }
//
//            })
        }
    }
}